//
//  XYViewController.m
//  OrientationTest
//
//  Created by yao zhaofeng on 2018/6/20.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "XYViewController.h"

@interface XYViewController ()
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) dispatch_queue_t myQueue;
@property (nonatomic, strong) dispatch_group_t myGroup;
@property (nonatomic, strong) id observer;
@end
static int times = 0;

@implementation XYViewController

- (void)dealloc {
    NSLog(@"dealloc");
}

- (instancetype)init {
    if (self = [super init]) {
        _myQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
        _myGroup = dispatch_group_create();
    }
    
    return self;
}

- (void)loadView {
    [super loadView];
    
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _webView.userInteractionEnabled = NO;
    [self.view addSubview:_webView];
//    NSURL *url = [NSURL URLWithString:@"https://club.jj.cn/jjinfo.html"];
//    NSURL *url = [NSURL URLWithString:@"https://mobtest.my.jj.cn/actv_html/2021/0607exchange_code/test.html"];
    NSURL *url = [NSURL URLWithString:@"https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2018103061934387&biz_content=%7B%22certify%5Fid%22%3A%222107873c5e42dbfb4dfbdadfe1fec3ef%22%7D&charset=utf-8&method=alipay.user.certify.open.certify&sign_type=RSA2&timestamp=2021-11-02 11:16:20&version=1.0&sign=Dm5HXVgQ3vi%2FPnYxqzd8OYyDGO9rw3p2F6nsHe6ocDe1iqLnxbj%2FRFOto1IuGbznL%2FUn7Ns9yU3UcW6L19wO7tL6QPQaXeS5vPVZUpgKyiPn8Im36Y%2F2qv%2F%2BVCLK3BccXi3nKrBNIGfQvtJfCcr2kb2L1W3iDvd8cmDEa0loIz%2B3CbfXY6oFHMoIq0%2FhOdG9HYqojGx%2FxsBdh1qtX82%2FrpEthIqQZkZXfeJSlv9TFWcUh8E8vKLRStJGODJZwB%2FwgCy3mZ2P4jWfSIrWCGOFfU9sjSeNdo6eOwZIrBk8BdNjuonkb71yURwshuiLox8u2KUL5nK%2F55hB%2FefwpAyywg%3D%3D"];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    _webView.scalesPageToFit = YES;
//    _webView.scrollView.scro
    [_webView loadRequest:urlRequest];
//    if (@available(iOS 11.0, *)) {
//        _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(0, 0, 100, 100);
    [btn addTarget:self action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"点我" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:btn];
}

- (void)viewWillLayoutSubviews {
    [_webView setFrame:self.view.bounds];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
//    dispatch_group_async(_myGroup, _myQueue, ^{
//        [self doSomething];
//    });
//    _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey"
//                                                                  object:nil
//                                                                   queue:nil
//                                                              usingBlock:^(NSNotification *note) {
//                                                                  [self doSomething];
//                                                              }];
//
//    NSString *temp =nil;
//    temp = [temp stringByReplacingOccurrencesOfString:@"." withString:@""];
//    int version = atoi([temp cStringUsingEncoding:NSUTF8StringEncoding]);
//    NSLog(@"version is %d", version);
    // Do any additional setup after loading the view.
}

- (void)doSomething {
    NSLog(@"doSomething");
//    [self dismissViewControllerAnimated:NO completion:nil];
    self.isLandScape = !_isLandScape;
    __weak XYViewController *weakSelf = self;
    XYViewController *tempVC = [XYViewController new];
   tempVC.modalPresentationStyle = UIModalPresentationFullScreen;
   [self presentViewController:tempVC animated:NO completion:^{
       [weakSelf dismissViewControllerAnimated:NO completion:nil];
   }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (BOOL)prefersStatusBarHidden {
    return YES;
}

//- (BOOL)shouldAutorotate {
//    return NO;
//}

//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskLandscape;
////    return UIInterfaceOrientationMaskAll;
//}
//
///**
// *文档关于UIInterfaceOrientationLandscapeLeft的说明：The device is in landscape mode, with the device held upright and the Home button on the right side.
// *实际验证的时候发现，home应该是在左侧的，这块不知道是不是注释错误了~~
// *
// */
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    return UIInterfaceOrientationLandscapeLeft;
//}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (_isLandScape) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return  UIInterfaceOrientationMaskPortrait;
    }
    
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if (_isLandScape) {
        return UIInterfaceOrientationLandscapeLeft;
    } else {
        return  UIInterfaceOrientationPortrait;
    }

}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self dismissViewControllerAnimated:NO completion:nil];
//    [self.navigationController popViewControllerAnimated:YES];
    
    XYViewController *XYVC = [[XYViewController alloc] init];
    XYVC.modalPresentationStyle = UIModalPresentationFullScreen;
    if (times%2 == 0) {
//        [self.navigationController pushViewController:XYVC animated:YES];
        [self presentViewController:XYVC animated:NO completion:nil];
    } else {
        XYVC.isLandScape = YES;
        [self presentViewController:XYVC animated:NO completion:nil];
    }
    times++;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

}
@end
